package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.DiscountRuleEnum;
import com.sgzn.sgpay.enums.ReturnResultState;
import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsMerchantDiscountRuleAddRequest;
import com.sgzn.sgpay.model.request.SgsMerchantDiscountRuleQueryByMerchantIdRequest;
import com.sgzn.sgpay.model.request.SgsMerchantDiscountRuleQueryRequest;
import com.sgzn.sgpay.model.request.SgsMerchantDiscountRuleUpdateRequest;
import com.sgzn.sgpay.model.request.dto.DiscountRuleDto;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.persistence.dao.TMerchantDiscountRuleMapper;
import com.sgzn.sgpay.persistence.domain.TMerchantDiscountRule;
import com.sgzn.sgpay.persistence.domain.TMerchantDiscountRuleExample;
import com.sgzn.sgpay.service.SgsMerchantDiscountRuleService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 商户消费优惠规则接口实现类
 * @author huangsenguang
 * @version Id: SgsMerchantDiscountRuleServiceImpl.java, v 0.1 2018/6/3 16:35 huangsenguang Exp $$
 */
@Service
@Slf4j
public class SgsMerchantDiscountRuleServiceImpl implements SgsMerchantDiscountRuleService {
    @Autowired
    private TMerchantDiscountRuleMapper tMerchantDiscountRuleMapper;

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SgsResultResponse<String> addMerchantDiscountRule(SgsMerchantDiscountRuleAddRequest request) {
        int num = 0;
        for(DiscountRuleEnum discountRuleEnum:DiscountRuleEnum.values()){
            TMerchantDiscountRule tMerchantDiscountRule = new TMerchantDiscountRule();
            tMerchantDiscountRule.setMerchantId(request.getMchId());
            tMerchantDiscountRule.setDiscountLevel(discountRuleEnum.getDiscountLevel());
            tMerchantDiscountRule.setDiscountAmount(discountRuleEnum.getDiscountAmount());
            tMerchantDiscountRule.setCreatedBy(request.getMchId());
            tMerchantDiscountRule.setCreatedAt(new Date());
            tMerchantDiscountRule.setUpdatedAt(new Date());
            try {
                num = tMerchantDiscountRuleMapper.insertSelective(tMerchantDiscountRule);
            } catch (DataAccessException e) {
                log.error("插入响应流水日志异常.{}", e);
            }
        }
        if (num == 1) {
            return SgsResponseUtil.INSTANCE.success(ReturnResultState.SUCCESS.getCode());
        } else {
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C503.getResponseCode(),SgsResponseCode.C503.getResponseDesc(), ReturnResultState.FAIL.getCode());
        }
    }


    @Override
    public SgsResultResponse<List<TMerchantDiscountRule>> queryMerchantDiscountList(SgsMerchantDiscountRuleQueryRequest request) {
        TMerchantDiscountRuleExample tMerchantDiscountRuleExample = new TMerchantDiscountRuleExample();
        tMerchantDiscountRuleExample.createCriteria().andMerchantIdEqualTo(request.getMchId());
        List<TMerchantDiscountRule> tMerchantDiscountRules = tMerchantDiscountRuleMapper.selectByExample(tMerchantDiscountRuleExample);
        return SgsResponseUtil.INSTANCE.success(tMerchantDiscountRules);
    }

    @Override
    public SgsResultResponse<List<TMerchantDiscountRule>> queryByMerchantId(SgsMerchantDiscountRuleQueryByMerchantIdRequest request) {
        TMerchantDiscountRuleExample tMerchantDiscountRuleExample = new TMerchantDiscountRuleExample();
        tMerchantDiscountRuleExample.createCriteria().andMerchantIdEqualTo(request.getMchId());
        StringBuffer clause = new StringBuffer();
        clause.append("discount_level asc ");
        tMerchantDiscountRuleExample.setOrderByClause(clause.toString());
        List<TMerchantDiscountRule> tMerchantDiscountRules = tMerchantDiscountRuleMapper.selectByExample(tMerchantDiscountRuleExample);
        return SgsResponseUtil.INSTANCE.success(tMerchantDiscountRules);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SgsResultResponse<String> updateMerchantDiscountRule(SgsMerchantDiscountRuleUpdateRequest request) {
        boolean flag = true;
        for (DiscountRuleDto discountRuleDto : request.getRuleList()) {
            TMerchantDiscountRule record = new TMerchantDiscountRule();
            record.setId(discountRuleDto.getId());
            record.setDiscountAmount(discountRuleDto.getDiscountAmount());
            record.setUpdatedBy(request.getMchId());
            record.setUpdatedAt(new Date());
            TMerchantDiscountRuleExample example = new TMerchantDiscountRuleExample();
            TMerchantDiscountRuleExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(discountRuleDto.getId());
            int num = tMerchantDiscountRuleMapper.updateByExampleSelective(record, example);
            if (num != 1){
                flag = false;
                break;
            }
        }
        if (flag) {
            return SgsResponseUtil.INSTANCE.success(ReturnResultState.SUCCESS.getCode());
        } else {
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C503.getResponseCode(),SgsResponseCode.C503.getResponseDesc(), ReturnResultState.FAIL.getCode());
        }
    }
}